package beans;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Account {
	private int id;
	private String username;
	private String displayName;
	private String type;
	private String avatar;
	private int state;
	private int numPosts;
	private String sex;
	private Date birthday;
	private Date join;
	private String job;
	private String school;
	private String Email;
	private String address;
	private DateFormat format;
	public static final String PATTERN_VI = "dd/MM/yyyy";
	public static final String PATTERN_US = "MM/dd/yyyy";

	public Account(int id, String username, String displayName, String type,
			String avatar, int state, int numPosts) {
		this(id, username, displayName, type, state);
		this.avatar = avatar;
		this.numPosts = numPosts;
	}

	public Account(int id, String username, String displayName, String type,
			String avatar, int state, String join) throws ParseException {
		this(id, username, displayName, type, state);
		this.avatar = avatar;
		this.join = format.parse(join);
	}

	@Override
	public String toString() {
		return "Account [id=" + id + ", username=" + username
				+ ", displayName=" + displayName + ", type=" + type
				+ ", state=" + state + ", join=" + join + "]";
	}

	public Account(int id, String username, String displayName, String type,
			int state) {
		this.format = new SimpleDateFormat(PATTERN_VI);
		this.id = id;
		this.username = username;
		this.displayName = displayName;
		this.type = type;
		this.state = state;
	}

	public Account() {

	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getDisplayName() {
		return displayName;
	}

	public void setDisplayName(String displayName) {
		this.displayName = displayName;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public int isState() {
		return state;
	}

	public void setState(int state) {
		this.state = state;
	}

	public String getSex() {
		return sex;
	}

	public void setSex(String sex) {
		this.sex = sex;
	}

	public Date getBirthday() {
		return birthday;
	}

	public String getBirthdayString() {
		if (birthday != null)
			return format.format(birthday);
		return null;
	}

	public void setBirthday(Date birthday) {
		this.birthday = birthday;
	}

	public Date getJoin() {
		return join;
	}

	public String getJoinString() {
		return format.format(join);
	}

	public void setJoin(Date join) {
		this.join = join;
	}

	public void setJoin(String dateString) {
		try {
			this.join = format.parse(dateString);
		} catch (ParseException e) {
			e.printStackTrace();
		}
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getAvatar() {
		return (avatar == null || "".equals(avatar)) ? "default_avatar.png"
				: avatar;
	}

	public int getNumPosts() {
		return numPosts;
	}

	public void setNumPosts(int numPosts) {
		this.numPosts = numPosts;
	}

	public int getState() {
		return state;
	}

	public void setAvatar(String avatar) {
		this.avatar = avatar;
	}

	public String getJob() {
		return job;
	}

	public void setJob(String job) {
		this.job = job;
	}

	public String getSchoolName() {
		return school;
	}

	public void setSchool(String schoolName) {
		this.school = schoolName;
	}

	public String getEmail() {
		return Email;
	}

	public void setEmail(String email) {
		Email = email;
	}

	public String getAddress() {
		return address;
	}

	public void setAddresss(String placeHome) {
		this.address = placeHome;
	}

	public String getFullName() {
		return type.equalsIgnoreCase("user") ? this.username : this.username
				+ " - " + this.type;
	}

	public boolean isType(String type) {
		return this.type.equalsIgnoreCase(type);
	}

	public String formatDateSql(Date date) {
		return new SimpleDateFormat("yyyy-MM-dd").format(date);
	}

	public String getColor() {
		String result = "";
		switch (this.type) {
		case "Mod":
			result = "green";
			break;
		case "SMod":
			result = "blue";
			break;
		case "Admin":
			result = "red";
			break;
		}
		return result;
	}

	// FIXME
	public static Account createAccount(ResultSet rs, String id)
			throws SQLException {
		Account account = new Account(rs.getInt(id), rs.getString("username"),
				"null", rs.getString("type"), rs.getString("avatar"),
				rs.getInt("m_state"), rs.getInt("posts"));
		return account;
	}
}
